package com.dianrui.fmi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.Delegation;
import com.dianrui.fmi.util.MyUtil;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DelegationAdapter extends BaseAdapter {
    private List<Delegation> list;
    private Context context;
    private DelegationClickListener delegationClickListener;

    public DelegationAdapter(Context context, List<Delegation> list) {
        this.list = list;
        this.context = context;
    }

    public void refresh(List<Delegation> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    public interface DelegationClickListener {
        void onCancelClick(String symbolsId);
    }

    public void setOnDelegationClickListener(DelegationClickListener delegationClickListener) {
        this.delegationClickListener = delegationClickListener;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_delegation, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            Delegation delegation = list.get(position);
            holder.name.setText(delegation.full_symbol);
            if (delegation.direction.equals("1")) {
                holder.zfStatusImg.setImageResource(R.mipmap.arrow_up_red);
                holder.godTxt.setTextColor(parent.getContext().getResources().getColor(R.color.red1));
            }else {
                holder.zfStatusImg.setImageResource(R.mipmap.green_arrow_down);
                holder.godTxt.setTextColor(parent.getContext().getResources().getColor(R.color.green_2ab23c));
            }
            holder.createTime.setText(delegation.createtime);
            holder.godTxt.setText(delegation.number);
            if (delegation.earnings.contains("-")) {
                holder.godPriceTxt.setTextColor(parent.getContext().getResources().getColor(R.color.green_2ab23c));
            }else {
                holder.godPriceTxt.setTextColor(parent.getContext().getResources().getColor(R.color.red1));
            }
            BigDecimal num4 = new BigDecimal(delegation.earnings);
            double f3 = num4.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            holder.godPriceTxt.setText(MyUtil.formatBy2double(f3, 2) + "");
            holder.inventoryTxt.setText(delegation.postion);

            if (delegation.mix_price.equals("N/A")) {
                holder.mixPrice.setText("--");
            }else {
                holder.mixPrice.setText(delegation.mix_price);
            }
            holder.serviceTxt.setText(delegation.service_charge);
            holder.startPrice.setText(delegation.start_price);
            holder.price.setText(delegation.price);

        }
        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.zf_status_img)
        ImageView zfStatusImg;
        @BindView(R.id.god_txt)
        TextView godTxt;
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.god_price_txt)
        TextView godPriceTxt;
        @BindView(R.id.start_price)
        TextView startPrice;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.commision_price)
        TextView commisionPrice;
        @BindView(R.id.mix_price)
        TextView mixPrice;
        @BindView(R.id.service_txt)
        TextView serviceTxt;
        @BindView(R.id.inventory_txt)
        TextView inventoryTxt;
        @BindView(R.id.close_out)
        Button closeOut;
        @BindView(R.id.cancel)
        Button cancel;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
